package cn.jly.flink.operators;

import cn.jly.flink.entity.Employee;
import cn.jly.flink.utils.ConstantUtils;
import cn.jly.flink.utils.FlinkUtils;
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.java.ExecutionEnvironment;
import org.apache.flink.util.Collector;

import java.math.BigDecimal;

/**
 * DataSet API
 *
 * @PackageName cn.jly.flink.operators
 * @ClassName FlatMapDemo
 * @Description 测试flatMap算子
 * @Author 姬岚洋
 * @Date 2021/1/8 下午 4:42
 */
public class FlatMapDemo {
    public static void main(String[] args) throws Exception {
        final ExecutionEnvironment env = FlinkUtils.getBatchExecutionEnv();

        env.fromElements(
                new Employee(ConstantUtils.ID_GENERATOR.getAndIncrement(), "tom", BigDecimal.valueOf(10000)),
                new Employee(ConstantUtils.ID_GENERATOR.getAndIncrement(), "lily", BigDecimal.valueOf(20000)),
                new Employee(ConstantUtils.ID_GENERATOR.getAndIncrement(), "james", BigDecimal.valueOf(30000))
        )
                .flatMap(new FlatMapFunction<Employee, Employee>() {
                    @Override
                    public void flatMap(Employee employee, Collector<Employee> collector) throws Exception {
                        // salary greater than 20000
                        if (employee.getSalary().doubleValue() > 20000d) {
                            collector.collect(employee);
                        }
                    }
                })
                .print();
    }
}
